<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/23
 * Time: 17:08
 */

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/../Workerman/Autoloader.php';

$worker = new Worker();

/**
 * aes加密
 * @param $str
 * @return string
 */
function aesEncrypt($str,$ak,$sk)
{
    return base64_encode(openssl_encrypt($str,'AES-128-CBC',$ak, 1, $sk));
}

/**
 * aes解密
 * @param $str
 * @return string
 */
function aesDecrypt($str,$ak,$sk)
{
    return openssl_decrypt(base64_decode($str), 'AES-128-CBC',$ak, 1, $sk);
}

function createNonce($len){
    $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
    return substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),$len);
}
/*$ak = 'asdawsfesadfgews';
$sk = '3333111111111112';*/
/*$ak = 'VkhRW0C521ZMYuqF';
$sk = 'CE0J99-gsI-acW1P';*/
$ak = 'AsdafDesfe@234&!sd';
$sk = 'sWfsgsrdg#@4344*';
/**创建uuid
 * @return string
 */
 function  uuid()
{
    $chars = md5(uniqid(mt_rand(), true));
    return $chars;
}
$worker->onWorkerStart = function($worker)use($ak){
    $local = '127.0.0.1:9501';
    $online = '129.211.114.112:55049';
    $online  = '192.168.2.80:9505';
    $test = '192.168.2.79:8004';
    $online  ='13.56.182.31:8002';
    $con = new AsyncTcpConnection('ws://'.$online);
    $con->headers = array('access_key' =>$ak);
    $con->onConnect = function($con) {
   # $appUserId = 'wW1QL5f641b4b69a6f';
    # $appUserId = 'wW1QL5f641b4b69a6f';
    #   $appUserId = 'PeA3h5f642871c4642';
   #   $appUserId = 'Cl5kp5f6af6ece2544';
   #  $appUserId = 'T7559503';
      # $appUserId = 'T157249531';
        #$appUserId = 'pSbnr5f64609876698';
        #$appUserId = 'pSbnr5f64609876698';
      # $appUserId = 'I6q1p5f6460e4346bc';
     #  $appUserId = 'T891194838';
     #  $appUserId = 'Et2j15f718ebbb746e';
       #$appUserId = '5vG8O5f6421234e55b';
     #$appUserId = 'pSbnr5f64609876698';
     #  $appUserId = 'jianjiantest';
     #   $appUserId = '1Wdr25f64903e549b1';
     # $appUserId = 'qkSwA5f3deb5482085';
      # $appUserId = '80utM5f3de521803b9';
     # $appUserId = 'yR7Y25f3f92866777f';
      # $appUserId = 'T827892944';
     #$appUserId = 'pSbnr5f64609876698';
        #$appUserId = 'fKYAJ5f64246fe7f59';
        #$appUserId = 'mBRW55f7192087f684';
       # $appUserId = 'PeA3h5f642871c4642';
       # $appUserId = 'cRVle5f7eb9be9153d';
        #$appUserId = 'mBRW55f7192087f684';
       # $appUserId = 'ljLoV5f71920411f55';
      # $appUserId = 'pSbnr5f64609876698';
        $appUserId = '924220';
        #$appUserId = 'pSbnr5f64609876698';
        /*$ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';*/
        $ak = 'AsdafDesfe@234&!sd';
        $sk = 'sWfsgsrdg#@4344*';
     /*   $ak = 'VkhRW0C521ZMYuqF';
        $sk = 'CE0J99-gsI-acW1P';*/
        #ping
      /*  $form = array(
            'ping'
        );
        $action = 'ping';
        #发送ping
        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/

           #变更账户
       /* $form = array(
            'user_id'=>17,
            'nickname'=>'张三',
            'head'=>'http://pic2.sc.chinaz.com/Files/pic/pic9/202006/bpic20542_s.jpg'
        );
        $action = 'updateUserInfo';

        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/

            #登录id29
            $extra = array(
                'access_key'=>$ak,
                'app_user_id'=>$appUserId,
               # 'nickname'=>'15959285805',
                'head'=>'',
              # 'token'=>'2431601367933'
            );
            $action = 'login';
            $message = array(
                'nonce'=>time(),
                'content'=>' ',
                'action'=>$action,
                'extra'=>$extra
            );

            $message = array_merge($message,    $extra);
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));

        $time_interval = 6;
        $i = 1;
        $recent = 0;
        \Workerman\Timer::add($time_interval, function() use($ak,$sk,$con,$i,$appUserId,$recent)
        {
            #最近联系人
            $extra = array(
            );
            $action = 'recent';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));

            #七牛授权
           /*$extra = array(

            );
               $action = 'qiniuAuth';

            $message = array(
                'nonce'=>time(),
                'content'=>'DxOHay1593939803100.556885',
               'action'=>$action,
               'extra'=>$extra,
            );*/
            #单聊
 /*    $extra = array(
            'domain'=>1
            );
            $action = 'chat';
            $message = array(
                'nonce'=>time(),
                'content'=>'helloZCasdcvfsdf',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'ltVfC5f64218dba472',
                'type'=>1,
            );*/
     #消息撤回
         /* $extra = array(
                'msg_id'=>25125
            );
            $action = 'recall';
            $message = array(
                'nonce'=>uuid(),
                'content'=>'helloZCasdcvfsdf',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'T827892944',
                'type'=>1,
            );*/
      /*$extra = array(
            'domain'=>1
            );
            $action = 'chat';

            $message = array(
                'nonce'=>time(),
                'content'=>'helloZCasdcvfsdf',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'T827892944',
                'type'=>1,
            );*/
        #同步系统消息
          /*  $extra = array(
                'msg_id'=>1315
            );
            $action = 'syncSysMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #一对一系统消息
           /* $extra = array(
                'asd'=>'fuck'
            );
            $action = 'systemMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'9a4297c33d9328934e496a4074863cfs',
                'type'=>1,
            );*/

        #群聊

          #  file_name\":\".mid.txt\",\"etag\":\"Fg8JoT43IgOeWSIdrTXSpBCAmZcO\",\"file_size\":149
    /*  $extra = array(
             'file_name'=>'mid.txt',
             'etag'=>'Fg8JoT43IgOeWSIdrTXSpBCAmZcO',
             'file_size'=>149
            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time().mt_rand(10000,99999),
                'content'=>'群聊自动化测试'.date("Y-m-d H:i:s"),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>3422,
                'type'=>21,
            );*/

          #{"type":1,"group_id":"19","content":"a","action":"groupChat","nonce":"1594191881721FQK5l9"}
        /*$i =2445; 2560
        while ($i<2455){
            $extra = array(

            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time().mt_rand(10000,99999),
                'content'=>'群聊自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>$i,
                'type'=>1,
            );
            sleep(mt_rand(0,1));
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            echo $i.'群聊发送成功'.PHP_EOL;
            $i = $i+1;
        }*/
//图片转base64
        #上报终端信息
         /*  $extra = array(
               'token'=>'asdfesegfwergerg',
                'os'=>1,
                'model'=>'小米2',
                'language'=>'zh_cn',
                'showpreview'=>1,
            );
            $action = 'deviceinfo';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

    #创建群
     /* $img = file_get_contents('base64_2');
    $extra = array(
        'app_user_ids'=>'T7559503,T157249531,PeA3h5f642871c4642',
        'cover'=>$img,
        'group_name'=>'asdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG'
            );
            $action = 'createGroup';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
/*        $i = 0;
        while ($i<100){
            $extra = array(
                'app_user_ids'=>'T7559503,T157249531'
            );
            $action = 'createGroup';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            sleep(1);
            $i = $i+1;
        }*/
       #邀请入群
   /*  $extra = array(
                'app_user_id'=>'T7559503',
                'app_user_ids'=>'T853107411',
                'group_id'=>'2969'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #我的群
    /*   $extra = array(
                'type'=>0,
                'page'=>1,
                 'limit'=>10,
                #'updated_at'=>1599817642000
            );
            $action = 'myGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #群成员详情
       /* $extra = array(
                'group_id'=>'3427',
                'page'=>1,
               'limit'=>10,
              #   'updated_at'=>1597306690000
            );
            $action = 'groupMemberList';

            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/
        #编辑群
          /* $extra = array(
                'group_id'=>'2729',
                'group_name'=>23543653436,
                'invite_mode'=>"1"
            );
            $action = 'editGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'2634',
                'type'=>1,
            );*/
            #获取客服
        /*  $action = 'getServiceGroup';
            $extra = array(

            );
            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/

            #移除群成员
       /*$extra = array(
                'group_id'=>'2730',
                'app_user_ids'=>'T7559503,T157249531'
            );
            $action = 'removeMember';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           #退出群
    /*  $extra = array(
                'group_id'=>'2761',
            );
            $action = 'leaveGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #邀请入群
       /*    $extra = array(
                'group_id'=>'2656',
                'app_user_ids'=>'5wZQV5f6af6ed5afc0'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #扫码入群
        /*    $extra = array(
                'group_id'=>'2921',
                'app_user_id'=>'OHost5f6460f14f255'
            );
            $action = 'invitedToGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
/*  $extra = array(
                'group_id'=>'3',
                'app_user_ids'=>'jianjianxianshen1g'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #获取群历史消息
  /*    $extra = array(
                'group_id'=>'3153',
             #  'msg_id'=>2000,
              'count'=>20
            );
            $action = 'groupChatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

       #获取单聊历史消息
 /*$extra = array(
                'app_user_id'=>'I6q1p5f6460e4346bc',
                # 'msg_id'=>'33007',
                'count'=>10
            );
            $action = 'chatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #获取群详情
       /*  $extra = array(
                'group_id'=>'2798'
            );
            $action = 'getGroupDetail';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
  #主动发送系统消息
      /* $id = 20283;
          $extra = array(
                'msg_id'=>$id
            );
            $action = 'pushMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         /*   $extra = array(
            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time().mt_rand(10000,99999),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>$id,
                'type'=>1,
            );
            sleep(mt_rand(0,2));
            $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            $id = $id+1;
            echo '发送'.PHP_EOL;*/

        #获取客服群getServiceGroup
     /*    $extra = array(
            );
            $action = 'getServiceGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #客服发消息
     /*  $extra = array(

            );
            $action = 'groupChat';

            $message = array(
                'nonce'=>time().'asfse',
                'content'=>'您好，客服售前咨询-高蚁材为您服务。😥🤫😪😪🥳💘😪🤔🤔🤔🤔'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'106',
                'type'=>1,
            );*/
     #修改群信息
         /*$extra = array(
                'group_id'=>'561',
                'group_name'=>'群的阿萨德1111',
                 'banned_to_post'=>'1',
                'invite_mode'=>'0',
            );
            $action = 'editGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #群管理员设置
         /*   $extra = array(
                'group_id'=>'177',
                'role'=>2,
                'app_user_ids'=>'qkSwA5f3deb5482085,79HF55f3deccdd4226'
            );
            $action = 'groupMemberSetting';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #创建黑名单
          /*  $extra = array(
                'app_user_id'=>'T404895852'
            );
            $action = ' addBlacklist';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
          #删除黑名单
      /*   $extra = array(
             'app_user_id'=>'T404895852'
         );
            $action = 'delBlacklist';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #获取黑名单
         /* $extra = array(

        );
            $action = 'getBlacklist';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #群消息设置
         /* $extra = array(
                'group_id'=>2721,
                'msg_setting'=>1,
               'top'=>1,
              'nickname_in_group'=>'小白'
            );
            $action = 'groupSetting';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
          #主动同步信息
       /*$condition = array(
               [ 'type'=>1,
                   'id'=>'wW1QL5f641b4b69a6f'
               ],
           [ 'type'=>1,
               'id'=>'1Wdr25f64903e549b1'
           ]
            );
            $extra = array(
                'condition'=>$condition,
            );
            $action = 'syncLastMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #更新用户信息
           /* $extra = array(
                'nickname'=>'我的最帅的开发小哥',
                'head'=>'https://fanyiapp.cdn.bcebos.com/cms/image/58eb5ccdedfd017daa2e7d4e48d43891.jpg'
            );
            $action = 'updateUserInfo';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           #获取其他人用户信息
        /* $extra = array(
                'app_user_id'=>'jianjianxiansheng',
            );
            $action = 'getOtherUserInfo';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
            #清理消息
        /* $extra = array(
                'msg_type'=>1,
                'id'=>'WHQYb5f64526baa540',
            );
            $action = 'clearMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #自定义系统消息
      /*   $extra = array(
                'sub_type'=>'personal',
                'associated_id'=>'HP2RG5f6ef2636e233',
                'data'=>'asdfasfsef'
            );
            $action = 'diySysMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           $con->send(aesEncrypt(json_encode($message),$ak,$sk));
            echo '发送时间：'.date("Y-m-d H:i:s").PHP_EOL;
        });
    };

    $con->onMessage = function($con, $data) {
        /*$ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';*/

        /*$ak = 'VkhRW0C521ZMYuqF';
        $sk = 'CE0J99-gsI-acW1P';*/
        $ak = 'AsdafDesfe@234&!sd';
        $sk = 'sWfsgsrdg#@4344*';
        echo $data,PHP_EOL;

        $dataArr = json_decode(aesDecrypt($data,$ak,$sk),true);
        var_dump(   $dataArr);
        echo date('Y-m-d H:i:s',$dataArr['time']/1000).PHP_EOL;
        echo '接收时间：'.date("Y-m-d H:i:s").PHP_EOL;

       # file_put_contents('json',$dataArr['data'].PHP_EOL,FILE_APPEND);
        file_put_contents('json',json_encode($dataArr).PHP_EOL,FILE_APPEND);
        echo PHP_EOL;
    };

    $con->connect();
};

Worker::runAll();